package org.bigjava.novelplatform.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileReader;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.bigjava.novelplatform.common.constant.NovelInfoConstant;
import org.bigjava.novelplatform.common.enums.ActionType;
import org.bigjava.novelplatform.common.enums.SigningState;
import org.bigjava.novelplatform.common.enums.UserRole;
import org.bigjava.novelplatform.common.exception.UserActionException;
import org.bigjava.novelplatform.common.utils.*;
import org.bigjava.novelplatform.entity.*;
import org.bigjava.novelplatform.mapper.*;
import org.bigjava.novelplatform.service.CommonService;
import org.bigjava.novelplatform.vo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import static org.bigjava.novelplatform.common.exception.ExceptionCodeMessage.ILLEGAL_OPERATION;

/**
 * @author ykh
 * @create 2022-03-23 21:58
 */
@Slf4j
@Service
public class CommonServiceImpl implements CommonService {
    @Resource
    private BooksMapper booksMapper;

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private WriterMapper writerMapper;

    @Resource
    private CatalogMapper catalogMapper;

    @Resource
    private ChapterMapper chapterMapper;

    @Resource
    private BookRackMapper bookRackMapper;

    @Resource
    private PurchasedChapterMapper purchasedChapterMapper;

    @Resource
    RedisUtils redisUtils;

    @Resource
    private ChapterSaysMapper chapterSaysMapper;

    @Resource
    CommentActionMapper commentActionMapper;


    @Override
    public List<SearchBriefInfoVo> getSuggestVo() {
        //查询书籍热度前三
        List<Books> books = booksMapper.listByVisit(3);

        //查询作者积分前三,直接通过sql查询
        List<Users> users = writerMapper.listUserByIntegral(3);
        //将信息转化为vo

        List<SearchBriefInfoVo> userVo = EntityTranslatorVo.toSearchBriefInfoVo(users, "id", "nickName",
                NovelInfoConstant.USER);
        List<SearchBriefInfoVo> BookVo = EntityTranslatorVo.toSearchBriefInfoVo(books, "id", "bookName",
                NovelInfoConstant.BOOK);

        userVo.addAll(BookVo);

        return userVo;
    }

    @Override
    public List<SearchBriefInfoVo> getSuggestVoByInput(String input) {
        if (!StringUtils.isEmpty(input)) {
            List<Books> books = booksMapper.listByLike(input);
            List<Users> writer = usersMapper.listWriterByLike(input, UserRole.WRITER);
            //将信息转化为vo
            List<SearchBriefInfoVo> BookVo = EntityTranslatorVo.toSearchBriefInfoVo(books, "id", "bookName",
                    NovelInfoConstant.BOOK);

            List<SearchBriefInfoVo> writerVo = EntityTranslatorVo.toSearchBriefInfoVo(writer, "id", "bookName",
                    NovelInfoConstant.WRITER);

            BookVo.addAll(writerVo);
            return BookVo;
        }
        log.warn("警告：接受到一个预期之外的参数{}", input);
        return null;
    }

    @Override
    public List<Catalog> selectAllChildCatalog(String parent) {
        return catalogMapper.selectByParentName(parent);
    }

    @Override
    public Integer selectScoreRanking(Integer bookId) {
        //查询当前月度积分
        Integer integrate = booksMapper.selectIntegralById(bookId);
        //查询大于当前月度积分的总人数
        Integer ranking = booksMapper.selectScoreRankingByIntegrate(integrate);
        return ranking;
    }

    @Override
    public BookInfoVo getBookInfo(Integer bookId) {

        Books books = booksMapper.selectBookById(bookId);

        //作者信息
        Writer writer = writerMapper.selectById(books.getWriterId());
        WriterInfoVo writerInfoVo = getWriterInfo(books.getWriterId());

        //最新10章
        List<Chapter> chapters = chapterMapper.selectLastByBookId(bookId, 10);
        List<ChapterVo> chapterVoList = chapters.stream().map((t) -> {
            ChapterVo chapterVo = new ChapterVo();
            chapterVo.setId(t.getId());
            chapterVo.setSort(t.getChapterOrder());
            chapterVo.setTitle(t.getTitle());
            chapterVo.setUpdateTime(t.getUpdateTime());
            return chapterVo;
        }).collect(Collectors.toList());

        //月度积分排行
        Integer ranking = selectScoreRanking(bookId);
        //积分差
        Integer integralDifference = selectIntegralDifference(bookId);

        //分类
        Catalog catalog = catalogMapper.selectByName(books.getCatalogName());
        Catalog parenCatalog = catalogMapper.selectById(catalog.getParenCatalogId());
        /*
        粉丝榜前10
         */
//        List<Users> usersList = bookRackMapper.selectFanRanking(bookId, 10);
//        List<UserInfoVo> userInfoVos = usersList.stream().map((u) -> {
//            UserInfoVo userInfoVo = new UserInfoVo();
//            userInfoVo.setId(u.getId());
//            userInfoVo.setType(u.getUserType().toString());
//            userInfoVo.setNickName(u.getNickName());
//            userInfoVo.setHeadPortraitUrl(u.getHeadPortraitUrl());
//            return userInfoVo;
//        }).collect(Collectors.toList());
        List<BookInfoVo.Fans> fansList = usersMapper.selectBookFan(bookId, 10);


        BookInfoVo bookInfoVo = new BookInfoVo();
        //id
        bookInfoVo.setId(books.getId());
        //书名
        bookInfoVo.setBookName(books.getBookName());
        //简介
        bookInfoVo.setBriefIntroduction(books.getSynopsis());
        //字数
        bookInfoVo.setWords(books.getWordCount());
        //三级分类
        bookInfoVo.setCatalog(books.getCatalogName());
        //二级分类
        bookInfoVo.setParentCatalog(parenCatalog.getCatalogName());
        //章节数
        bookInfoVo.setNumberOfChapters(books.getChapter());
        //最后10章信息
        bookInfoVo.setChapterVosList(chapterVoList);
        //作者信息
        bookInfoVo.setWriterInfoVo(writerInfoVo);
        //封面
        bookInfoVo.setCoverUrl(books.getBookImgUrl());
        //连载状态
        bookInfoVo.setSerialStatus(books.getContinuedState());
        //签约状态
        bookInfoVo.setSigningStatus(writer.getSigning());
        //积分差
        bookInfoVo.setIntegralDifference(integralDifference);
        //粉丝排行
//        bookInfoVo.setFanRanking(userInfoVos);
        bookInfoVo.setFans(fansList);
        //月度上涨积分
        bookInfoVo.setRisingPoints(books.getRecentIntegral());
        //月度积分排名
        bookInfoVo.setScoreRanking(ranking);

        bookInfoVo.setIntegral(books.getIntegral());

        return bookInfoVo;
    }

    @Override
    public List<Catalog> getCatalogByLevel(Integer level) {

        return catalogMapper.selectByLevel(level);

    }


    @Override
    public List<CatalogVo> getCatalogAll() {
        List<CatalogVo> catalogVoList = redisUtils.parseJsonToList(NovelInfoConstant.RedisKey.CATALOG_ALL,
                CatalogVo.class);
        if (!CollectionUtils.isEmpty(catalogVoList)) {
            return catalogVoList;
        }
        catalogVoList = catalogMapper.getCatalogAll();
        redisUtils.set(catalogVoList, NovelInfoConstant.RedisKey.CATALOG_ALL, Duration.ofDays(1));
        return catalogVoList;
    }

    @Override
    public List<BookInfoVo> getWriterBook(Integer writerId) {
        //作者10本书
        List<Books> booksList = booksMapper.selectWriterBooks(writerId, 10);

        List<BookInfoVo> collect = booksList.stream().map(b -> {
            BookInfoVo bookInfoVo = new BookInfoVo();
            bookInfoVo.setBookName(b.getBookName());
            bookInfoVo.setCatalog(b.getCatalogName());
            bookInfoVo.setCoverUrl(b.getBookImgUrl());
            bookInfoVo.setId(b.getId());
            bookInfoVo.setBriefIntroduction(b.getSynopsis());
            return bookInfoVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public WriterInfoVo getWriterInfo(Integer writerId) {
        Writer writer = writerMapper.selectById(writerId);
        Users users = usersMapper.selectById(writer.getUserId());
        WriterInfoVo writerInfoVo = new WriterInfoVo();
        writerInfoVo.setBookCount(writer.getBooksCount());
        writerInfoVo.setHeadPortraitUrl(users.getHeadPortraitUrl());
        writerInfoVo.setId(writer.getId());
        writerInfoVo.setNickName(users.getNickName());
        //作者简介：数据库没有这个字段
//        writerInfoVo.setBriefIntroduction();
        Date registerWriterTime = writer.getRegisterWriterTime();
        writerInfoVo.setCreationDays((int) DateUtil.between(registerWriterTime, new Date(), DateUnit.DAY));
        writerInfoVo.setCumulativeNumberOfWords(writer.getWordCount());
        return writerInfoVo;
    }

    @Override
    public Integer selectIntegralDifference(Integer bookId) {
        Integer integer = booksMapper.selectIntegralById(bookId);

        Integer higherInteger = booksMapper.selectPriorByIntegral(integer);
        //没有比它还高的积分
        if (higherInteger == null) {
            //比当前积分低一位的积分
            Integer afterIntegral = booksMapper.selectAfterIntegral(integer);
            if (afterIntegral == null) {
                return 0;
            }
            //此时数值为负数
            return afterIntegral - integer;
        }
        return higherInteger - integer;
    }

    @Override
    public Page<SimpleBookVo> bookListPage(QueryCriteriaPlus queryCondition, Integer current, Integer size) {
        //让前端将所有的三件分类转化为
        //查询
        Page<SimpleBookVo> booksPage = booksMapper.selectBookPage(new Page<SimpleBookVo>(current, size),
                queryCondition);
        for (SimpleBookVo simpleBookVo : booksPage.getRecords()) {
            String writerName = writerMapper.selectWriterNameById(simpleBookVo.getWriterId());
            if (writerName != null) {
                simpleBookVo.setWriterName(writerName);
            }
            Catalog catalog = catalogMapper.selectParentName(simpleBookVo.getCatalog());
            if (catalog != null) {
                simpleBookVo.setParentCatalog(catalog.getCatalogName());
            }
        }
        return booksPage;
    }

    @Override
    public List<Books> bookList(String type, Integer limit) {
        List<Books> books = null;
        //热度为主的排名
        if (NovelInfoConstant.VISIT.equalsIgnoreCase(type)) {
            books = booksMapper.listByVisit(limit);
            //积分为主的排名
        } else if (NovelInfoConstant.INTEGRAL.equalsIgnoreCase(type)) {
            books = booksMapper.listByIntegral(limit);
            //订阅量
        } else if (NovelInfoConstant.SUBSCRIPTION.equalsIgnoreCase(type)) {
            books = booksMapper.listBySubscription(limit);
            //新书积分排行
        } else if (NovelInfoConstant.NEW_INTEGRAL.equalsIgnoreCase(type)) {
            books = booksMapper.listNewBookByIntegral(limit);
        }
        return books;
    }

    @Override
    public Page<BookOrderPageVo> selectBookListPage(String catalog, String type, Integer current, Integer size) {
        List<String> catalogs = null;
        if (!StringUtils.isEmpty(catalog)) {
            catalogs =
                    catalogMapper.selectChildrenByParenName(catalog).stream().map(Catalog::getCatalogName).collect(Collectors.toList());
        }
        Page<BookOrderPageVo> bookOrderPageVoPage = new Page<BookOrderPageVo>(current,
                size);
        if ("打赏榜".equalsIgnoreCase(type)) {
            bookOrderPageVoPage =  booksMapper.selectBookListPage(bookOrderPageVoPage,
                    catalogs, null, null,
                    null, "books.recent_integral", "books.recent_integral");
        } else if ("畅销榜".equalsIgnoreCase(type)) {
            bookOrderPageVoPage =  booksMapper.selectBookListPage(bookOrderPageVoPage,
                    catalogs, null, null,
                    null, "books.earnings", "books.earnings");
        } else if ("阅读指数榜".equalsIgnoreCase(type)) {
            bookOrderPageVoPage =  booksMapper.selectBookListPage(bookOrderPageVoPage,
                    catalogs, null, null,
                    null, "books.recent_visit", "books.recent_visit");
        } else if ("收藏榜".equalsIgnoreCase(type)) {
            bookOrderPageVoPage =  booksMapper.selectBookListPage(bookOrderPageVoPage,
                    catalogs, null, null,
                    null, "books.subscription", "books.subscription");
        } else if ("签约作者新书榜".equalsIgnoreCase(type)) {
            bookOrderPageVoPage =  booksMapper.selectBookListPage(bookOrderPageVoPage,
                    catalogs, newBookTime(), null,
                    SigningState.SIGNING, "books.recent_visit", "books.recent_visit");
        } else if ("新人签约新书榜".equalsIgnoreCase(type)) {
            bookOrderPageVoPage =  booksMapper.selectBookListPage(bookOrderPageVoPage,
                    catalogs, newBookTime(), newWriterTime(),
                    SigningState.SIGNING, "books.recent_visit", "books.recent_visit");
        } else if ("公众作者新书榜".equalsIgnoreCase(type)) {
            bookOrderPageVoPage =  booksMapper.selectBookListPage(bookOrderPageVoPage,
                    catalogs, newBookTime(), null,
                    SigningState.NOT_SIGNING, "books.recent_visit", "books.recent_visit");
        } else if ("新人作者新书榜".equalsIgnoreCase(type)) {
            bookOrderPageVoPage =  booksMapper.selectBookListPage(bookOrderPageVoPage,
                    catalogs, newBookTime(), newWriterTime(),
                    null, "books.recent_visit", "books.recent_visit");
        }
        bookOrderPageVoPage.getRecords().forEach((item)->{
            String catalogName = item.getCatalogName();
            item.setParentCatalog( catalogMapper.selectParentName(catalogName).getCatalogName() );
        });
        return bookOrderPageVoPage;
    }

    @Override
    public HashMap<String, List<BookOrderVo>> selectBookList(String catalog, Integer limit) {
        HashMap<String, List<BookOrderVo>> map = new HashMap<>(8);
        List<String> catalogs = null;
        if (!StringUtils.isEmpty(catalog)) {
            catalogs =
                    catalogMapper.selectChildrenByParenName(catalog).stream().map(Catalog::getCatalogName).collect(Collectors.toList());
        }
        //收藏榜
        List<BookOrderVo> booksList = booksMapper.selectBookList(catalogs, null, null, null,
                "books.subscription", "books.subscription",
                limit);

        map.put("subscription", otherSelect(booksList));
        //阅读指数榜
        List<BookOrderVo> booksList1 = booksMapper.selectBookList(catalogs, null, null, null,
                "books.recent_visit", "books.recent_visit",
                limit);
        map.put("recent_visit", otherSelect(booksList1));
        //打赏榜
        List<BookOrderVo> booksList2 = booksMapper.selectBookList(catalogs, null, null, null,
                "books.integral", "books.integral",
                limit);
        map.put("integral", otherSelect(booksList2));
        //月票榜
        List<BookOrderVo> booksList3 = booksMapper.selectBookList(catalogs, null, null, null,
                "books.recent_integral", "books.recent_integral",
                limit);
        map.put("recent_integral", otherSelect(booksList3));
        //畅销榜
        List<BookOrderVo> booksList4 = booksMapper.selectBookList(catalogs, null, null, null,
                "books.earnings", "books.earnings",
                limit);
        map.put("earnings", otherSelect(booksList4));
        //签约作者新书榜
        List<BookOrderVo> booksList5 = booksMapper.selectBookList(catalogs, newBookTime(), null, SigningState.SIGNING,
                "books.recent_visit", "books.recent_visit",
                limit);
        map.put("contractedWriterNewBook", otherSelect(booksList5));
        //公众作者新书榜
        List<BookOrderVo> booksList6 = booksMapper.selectBookList(catalogs, newBookTime(), null, SigningState.SIGNING,
                "books.recent_visit", "books.recent_visit",
                limit);
        map.put("publicNewBook", otherSelect(booksList6));
        //新人作者新书榜
        List<BookOrderVo> booksList7 = booksMapper.selectBookList(catalogs, null, newWriterTime(), null,
                "books.recent_visit", "books.recent_visit",
                limit);
        map.put("newWriterNewBook", otherSelect(booksList7));
        return map;
    }

    public List<BookOrderVo> otherSelect(List<BookOrderVo> list) {
        for (BookOrderVo bookOrderVo : list) {
            Integer writerId = bookOrderVo.getWriterId();
            bookOrderVo.setWriterName(writerMapper.selectWriterNameById(writerId));
            bookOrderVo.setParentCatalog(catalogMapper.selectParentName(bookOrderVo.getCatalogName()).getCatalogName());
        }
        return list;
    }

    /**
     * 定义新书籍的创建时间
     *
     * @return
     */
    public String newBookTime() {
        LocalDate localDate = LocalDate.now().minusMonths(3);
        return localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
    }

    /**
     * 定义新作者的创建时间
     *
     * @return
     */
    public String newWriterTime() {
        LocalDate localDate = LocalDate.now().minusYears(1);
        return localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
    }

    @Override
    public Page<Books> ranking(String catalog, String orderType, Integer current, Integer size) {
        List<String> catalogs =
                catalogMapper.selectChildrenByParenName(catalog).stream().map(Catalog::getCatalogName).collect(Collectors.toList());
        Page<Books> page = null;
        if (orderType.equals("subscription")) {//加入书架，视作订阅 --> 收藏榜
            page = booksMapper.selectOrder(new Page<Books>(current, size), catalogs, "subscription");
        } else if (orderType.equals("recent_visit")) {//近期访问人数 --> 阅读指数榜
            page = booksMapper.selectOrder(new Page<Books>(current, size), catalogs, "recent_visit");
        } else if (orderType.equals("integral")) {//由用户打赏获取积分 --> 打赏榜
            page = booksMapper.selectOrder(new Page<Books>(current, size), catalogs, "integral");
        } else if (orderType.equals("recent_integral")) {//近期积分上涨 --> 月票榜
            page = booksMapper.selectOrder(new Page<Books>(current, size), catalogs, "recent_integral");
        } else if (orderType.equals("earnings")) {//章节收益 --> 畅销榜
            page = booksMapper.selectOrder(new Page<Books>(current, size), catalogs, "earnings");
        }
        return page;
    }


    @Override
    public Page<Books> newBookRanking(String catalog, String orderType, Integer current, Integer size) {
        List<String> catalogs = catalogMapper.selectChildrenByParenName(catalog).stream().map(Catalog::getCatalogName).collect(Collectors.toList());
        Page<Books> booksPage = new Page<Books>(current, size);
        if (orderType.equals("1")) {//新书,签约作者，阅读指数
            booksPage = booksMapper.selectNewBookOrder(booksPage, catalogs, newBookTime(),
                    null,
                    SigningState.SIGNING);
        } else if (orderType.equals("2")) {//新书,未签约作者，阅读指数
            booksPage = booksMapper.selectNewBookOrder(booksPage, catalogs, newBookTime(),
                    null,
                    SigningState.NOT_SIGNING);
        } else if (orderType.equals("3")) {//新书，新人，未签约，阅读指数
            booksPage = booksMapper.selectNewBookOrder(booksPage, catalogs, newBookTime(),
                    newWriterTime(),
                    SigningState.NOT_SIGNING);
        } else if (orderType.equals("4")) {//新书，新人，已签约，阅读指数,
            booksPage = booksMapper.selectNewBookOrder(booksPage, catalogs, newBookTime(),
                    newWriterTime(),
                    SigningState.SIGNING);
        }
        return booksPage;
    }


    @Override
    public List<ChapterVo> getChapterContents(Integer bookId) {
        String key = RedisBuildKey.bindBookChapterInfo(bookId);
        //TODO:更新章节收费时注意需要重新设置，redis
        List<ChapterVo> chapterVos = redisUtils.parseJsonToList(key,
                ChapterVo.class);
        if (!CollectionUtils.isEmpty(chapterVos)) {
            return chapterVos;
        }
        List<Chapter> chapters = chapterMapper.selectByBookId(bookId);
        //查询试读
        QueryWrapper<Books> queryWrapper = new QueryWrapper<Books>().select("id", "promotion", "writer_id").eq("id",
                bookId);
        Books bp = booksMapper.selectOne(queryWrapper);
        Integer promotion = bp.getPromotion();
        chapterVos = chapters.stream().map((t -> {
            ChapterVo chapterVo = new ChapterVo();
            chapterVo.setSort(t.getChapterOrder());
            chapterVo.setTitle(t.getTitle());
            chapterVo.setUpdateTime(t.getUpdateTime());
            chapterVo.setId(t.getId());
            chapterVo.setPurchased(true);
            return chapterVo;
        })).collect(Collectors.toList());
        if (promotion != -1) {
            for (int i = promotion; i < chapterVos.size(); i++) {
                chapterVos.get(i).setPurchased(false);
            }
        }
        redisUtils.set(chapterVos, key, Duration.ofDays(5));
        return chapterVos;
    }

    /**
     * 获取书籍部分信息
     *
     * @param bookId
     * @return
     */
    @Override
    public BookInfoPortion getBookInfoportion(Integer bookId) {
        Books books = booksMapper.selectBookById(bookId);
        //作者信息
        WriterInfoVo writerInfoVo = getWriterInfo(books.getWriterId());


        //分类
        Catalog catalog = catalogMapper.selectByName(books.getCatalogName());
        Catalog parenCatalog = catalogMapper.selectById(catalog.getParenCatalogId());

        BookInfoPortion bookInfoPortion = new BookInfoPortion();
        bookInfoPortion.setId(books.getId());
        bookInfoPortion.setBookName(books.getBookName());
        bookInfoPortion.setCoverUrl(books.getBookImgUrl());
        bookInfoPortion.setSerialStatus(books.getContinuedState());
        bookInfoPortion.setCreateTime(books.getCreateTime());
        bookInfoPortion.setWriterInfoVo(writerInfoVo);
        bookInfoPortion.setParentCatalog(parenCatalog.getCatalogName());
        return bookInfoPortion;
    }

    /**
     * 获取章节信息
     *
     * @param bookId
     * @param chapterId
     * @return
     */
    @Override
    public String selectChapter(Integer bookId, Long chapterId) {
        Chapter chapter = chapterMapper.selectById(chapterId);


        if (chapter.getId().equals(chapterId)) {
            File file = new File(chapter.getChapterUrl());
            String string = FileReader.create(file).readString();
            return string;
        }
        throw new UserActionException(ILLEGAL_OPERATION);
    }

    @Override
    public ChapterOrderVo selectChapterOrder(Integer bookId, Integer chapterOrder) {
        Chapter chapter = chapterMapper.selectChapterOrder(bookId, chapterOrder);
        ChapterOrderVo chapterOrderVo = new ChapterOrderVo();
        chapterOrderVo.setBookId(chapter.getBookId());
        chapterOrderVo.setId(chapter.getId());
        chapterOrderVo.setUpdateTime(chapter.getUpdateTime());
        chapterOrderVo.setTitle(chapter.getTitle());
        chapterOrderVo.setParagraphCount(chapter.getParagraphCount());
        File file = new File(chapter.getChapterUrl());
        String neirong = FileReader.create(file).readString();
        chapterOrderVo.setChapterconten(neirong);
        Integer integer = booksMapper.selectFreeChapter(bookId);
        if(integer == -1 || integer >= chapterOrder ){
            chapterOrderVo.setPurchase(null);
        }
        chapterOrderVo.setPurchase(false);
        return chapterOrderVo;
    }

    /**
     * 通过章节id和段落序列id查询评论的用户
     *
     * @param chapterId
     * @param paragraphLocation
     * @return
     */
    @Override
    public List<UserchaptersaysVo> selectChapterSays(Long chapterId, Integer paragraphLocation) {
        List<ChapterSays> chapterSays = chapterSaysMapper.selectChapterSays(chapterId, paragraphLocation);

        return chapterSays.stream().map(c -> {
            UserchaptersaysVo userchaptersaysVo = new UserchaptersaysVo();
            userchaptersaysVo.setId(c.getId());
            userchaptersaysVo.setContent(c.getContent());
            userchaptersaysVo.setPublishTime(c.getPublishTime());

            //查询users的用户
            Users users = usersMapper.selectId(c.getAddressorId());
            userchaptersaysVo.setNickName(users.getNickName());
            userchaptersaysVo.setHeadPortraitUrl(users.getHeadPortraitUrl());
            userchaptersaysVo.setAddressorId(users.getId());

            //查询点赞或者点踩的动作
            CommentAction commentAction = commentActionMapper.selectByUserIdAndCommentId(users.getId(),c.getId());
            if (commentAction == null){
                userchaptersaysVo.setActionType(ActionType.CANCEL);
            }else {
                userchaptersaysVo.setActionType(commentAction.getActionType());
            }


            return userchaptersaysVo;
        }).collect(Collectors.toList());
    }

    /**
     * 通过章节id和父评论id查询评论的用户
     *
     * @param chapterId
     * @param conversationTargetId
     * @return
     */
    @Override
    public List<UserchaptersaysVo> selectChapterSaysZipin(Long chapterId, Integer conversationTargetId) {
        List<ChapterSays> chapterSays = chapterSaysMapper.selectChapterSaysZipin(chapterId, conversationTargetId);

        return chapterSays.stream().map(c -> {
            UserchaptersaysVo userchaptersaysVo = new UserchaptersaysVo();
            userchaptersaysVo.setId(c.getId());
            userchaptersaysVo.setContent(c.getContent());
            userchaptersaysVo.setPublishTime(c.getPublishTime());

            //查询users的用户
            Users users = usersMapper.selectId(c.getAddressorId());
            userchaptersaysVo.setNickName(users.getNickName());
            userchaptersaysVo.setHeadPortraitUrl(users.getHeadPortraitUrl());
            return userchaptersaysVo;
        }).collect(Collectors.toList());
    }

    @Override
    public Chapter selectChapterObj(Integer bookId, Long chapterId) {
        return chapterMapper.selectById(chapterId);
    }

    @Override
    public List<String> selectCatalogOne() {
        List<Catalog> catalogs = catalogMapper.selectByLevel(1);
        return catalogs.stream().map(Catalog::getCatalogName).collect(Collectors.toList());
    }
}
